﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace GabageFactory.Models
{
    public class MessageReposity : Reposity
    {
        public IEnumerable<Message> ListMessages()
        {
            var messages = from m in _dataContext.Messages
                            select m;
            foreach (Message m in messages)
            {
                yield return m;
            }
        }

        public IEnumerable<Message> QueryMessages(int factoryId, DateTime? beginTime, DateTime? endTime, string name)
        {
            name = string.IsNullOrEmpty(name) ? "" : name;
            DateTime begin = DateTime.Parse("1/1/1953"); ;
            DateTime end = DateTime.Parse("12/31/9999"); ;
            bool hasBegin = false;
            bool hasEnd = false;
            if (beginTime.HasValue)
            {
                begin = beginTime.Value;
                hasBegin = true;
            }
            if (endTime.HasValue)
            {
                end = endTime.Value;
                hasEnd = true;
            }

            var messages = from f in _dataContext.Messages
                            where f.content.Contains(name) &&
                            ((factoryId > 0) ? f.FactoryId == factoryId : 1 == 1) &&
                            (hasBegin ? f.beginTime >= begin : 1 == 1) &&
                            (hasEnd ? f.endTime <= end : 1 == 1)
                            orderby f.Id ascending
                            select f;
            foreach (Message m in messages)
            {
                yield return m;
            }
        }

        public IEnumerable<Message> AquireMessages(long factoryId, int type)
        {
           var messages = from f in _dataContext.Messages
                           where
                           (((factoryId > 0) ? f.FactoryId == factoryId : 1 == 1) || f.FactoryId == 0) &&
                           (f.endTime >= DateTime.Now) &&
                           (f.beginTime <= DateTime.Now) && (type >= 0 ? f.type == type : 1 == 1)
                           orderby f.priority descending
                           select f;

            foreach (Message m in messages)
            {
                yield return m;
            }
        }

        public Message GetMessageById(long id)
        {
            var message = from m in _dataContext.Messages
                          where id == m.Id
                          select m;

            return message.First();
        }

        public Message CreateMessage(Message message)
        {
            _dataContext.Messages.InsertOnSubmit(message);
            SubmitChanges();
            return message;
        }

        public bool DeleteMessage(Message message)
        {
            _dataContext.Messages.DeleteOnSubmit(message);
            SubmitChanges();
            return true;
        }

        public Message EditMessage(Message message)
        {
            SubmitChanges();
            return message;
        }

        public Message FindMessage(long factoryId, string attach)
        {
            var message = from m in _dataContext.Messages
                          where m.FactoryId == factoryId && m.attach == attach
                          select m;

            if (message != null && message.Count() > 0)
            {
                return message.First();
            }

            return null;
        }
    }
}